package cn.com.gridinfo.sooc.core.course.note.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.course.note.common.CodeConst;
import cn.com.gridinfo.sooc.core.course.note.dao.CourseDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 写笔记界面
 * Created by yingchunli on 2015/12/31.
 */
public class WriteNoteActiivity extends BaseSoocActivity {

    String kcid, pmlid, mlid, nrid, jname;//课程id,章id,节ID,素材ID,节名称
    CourseDao dao;
    @InjectView(R.id.from_writeNote)
    TextView from;//来自
    @InjectView(R.id.zishu_writeNote)
    TextView zishu;//字数
    @InjectView(R.id.sfgk_writeNote)
    CheckBox sfgk;//是否公开 0：不公开   1：公开（默认为公开）
    @InjectView(R.id.bjnr_writeNote)
    EditText bjnr;//笔记内容
    @InjectView(R.id.commit_writeNote)
    Button commitBtn;//提交

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_writenote);
        ButterKnife.inject(this);
        dao = new CourseDao(this);
        //获取参数 课程详情记录
        kcid = getIntent().getStringExtra("kcid");//课程ID
        pmlid = getIntent().getStringExtra("pmlid");//章 id
        mlid = getIntent().getStringExtra("mlid");//节 id
        nrid = getIntent().getStringExtra("nrid");//内容 id
        jname = getIntent().getStringExtra("jname");
        from.setText(jname);
        sfgk.setChecked(true);//设置默认公开
        commitBtn.setEnabled(false);


        //笔记提交搜索监听
        bjnr.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH)
                {
                    //执行搜索
                    onCommitClick();
                }
                return false;
            }
        });
    }

    /**
     * 监听笔记字数
     */
    @OnTextChanged(value = {R.id.bjnr_writeNote}, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterTextChanged() {

        if (StringUtils.isNull(bjnr.getText().toString().trim())) {
            commitBtn.setEnabled(false);
            zishu.setText(200 + "");
        } else {
            //笔记字数
            int bjzs = bjnr.getText().toString().trim().length();
            Log.d("笔记字数：" + bjzs);
            zishu.setText(200 - bjzs + "");
            commitBtn.setEnabled(true);
        }
    }

    /**
     * 提交按钮的点击
     */
    @OnClick(R.id.commit_writeNote)
    void onCommitClick() {
        Log.d("mlid->" + mlid);
        Log.d("nrid->" + nrid);
        Log.d("pmlid->" + pmlid);
        Log.d("是否公开->" + sfgk.isChecked());
        Log.d("笔记内容->" + bjnr.getText().toString().trim());
        String str = bjnr.getText().toString().trim();
        commitBtn.setEnabled(false);
        //笔记内容不为空
        if (!StringUtils.isNull(bjnr.toString().trim()) && bjnr.getText().toString().trim() != null) {
            //发送请求  提交笔记
            if (sfgk.isChecked()) {
                //公开笔记
                dao.writeNote(kcid, mlid, nrid, pmlid, "1", bjnr.getText().toString().trim(), CodeConst.COURESE_WRITE_NOTE);
            } else {
                //不公开笔记
                dao.writeNote(kcid, mlid, nrid, pmlid, "0", bjnr.getText().toString().trim(), CodeConst.COURESE_WRITE_NOTE);
            }
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            //请求成功
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case 1:
                        //写笔记发送成功
                        MessageUtils.showRightImageShortToast(this, "发送笔记成功！");
                        setResult(RESULT_OK);
                        finish();
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
        commitBtn.setEnabled(true);
    }

    @Override
    public String setupToolBarTitle() {
        return "写笔记";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }
}
